package kz.pompei.OneGwt.client.personrole.impl;

import static kz.pompei.OneGwt.cmodel.CommonUtil.isDefined;

import java.util.List;

import kz.pompei.OneGwt.client.personrole.PersonFormMed;
import kz.pompei.OneGwt.client.personrole.PersonListMed;
import kz.pompei.OneGwt.client.personrole.PersonListView;
import kz.pompei.OneGwt.cmodel.PersonRecord;
import kz.pompei.OneGwt.ifaces.util.Comeback;
import kz.pompei.OneGwt.ifaces.util.Handler;
import kz.pompei.OneGwt.ifaces.util.Handler1;
import kz.pompei.OneGwt.ifaces.util.Handler1List;
import kz.pompei.OneGwt.ifaces.util.HandlerKiller;
import kz.pompei.OneGwt.ifaces.widget.StateMessage;
import kz.pompei.OneGwt.remote.PersonroleManagerServiceAsync;

public class PersonListMedImpl implements PersonListMed {
  
  private PersonListView view;
  private PersonroleManagerServiceAsync service;
  private PersonFormMed personFormMed;
  
  private final Handler1List<String> personListClickHandlers = new Handler1List<String>();
  
  @Override
  public HandlerKiller addPersonSelectHandler(Handler1<String> handler) {
    return personListClickHandlers.append( handler );
  }
  
  public void setPersonFormMed(PersonFormMed personFormMed) {
    this.personFormMed = personFormMed;
  }
  
  public void setView(PersonListView view) {
    this.view = view;
  }
  
  public void setService(PersonroleManagerServiceAsync service) {
    this.service = service;
  }
  
  @Override
  public void refresh() {
    initialize();
    view.buttonAdd().setEnabled( false );
    view.buttonEdit().setEnabled( false );
    view.buttonDelete().setEnabled( false );
    view.listPerson().clean();
    view.listPerson().setStateMessage( StateMessage.WAIT );
    service.getPersonList( new Comeback<List<PersonRecord>>() {
      @Override
      protected void result(List<PersonRecord> result) {
        view.listPerson().set( result );
        
        updateEnabilityOfAddEditDelButtons();
      }
    } );
  }
  
  private boolean initialized = false;
  
  private void initialize() {
    if (initialized) return;
    defineEventHandlers();
    initialized = true;
  }
  
  private void defineEventHandlers() {
    view.buttonAdd().addClickHandler( new Handler() {
      @Override
      public void handle() {
        personFormMed.showForm( null, new Handler1<PersonRecord>() {
          @Override
          public void handle(PersonRecord t) {
            view.listPerson().addOrUpdate( t );
            view.listPerson().select( t.getId() );
          }
        } );
      }
    } );
    
    view.buttonEdit().addClickHandler( new Handler() {
      @Override
      public void handle() {
        personFormMed.showForm( view.listPerson().getSelectedId(), new Handler1<PersonRecord>() {
          @Override
          public void handle(PersonRecord t) {
            view.listPerson().addOrUpdate( t );
            view.listPerson().select( t.getId() );
          }
        } );
      }
    } );
    
    view.listPerson().addChangeSelectionHandler( new Handler() {
      @Override
      public void handle() {
        updateEnabilityOfAddEditDelButtons();
        personListClickHandlers.fire( view.listPerson().getSelectedId() );
      }
    } );
  }
  
  private void updateEnabilityOfAddEditDelButtons() {
    view.buttonAdd().setEnabled( true );
    boolean enabled = isDefined( view.listPerson().getSelectedId() );
    view.buttonEdit().setEnabled( enabled );
    view.buttonDelete().setEnabled( enabled );
  }
}
